// pages/register/register.js
import wxParse from "../../wxParse/wxParse.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: 1,
    isDisabled: false,
    tel: "",
    code: "",
    password: "",
    codeBtn: "获取验证码"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  mobileRegister: function() {
    this.setData({
      tabs: 1
    })
  },
  emailRegister: function() {
    this.setData({
      tabs: 2
    })
  },
  mobileValidate: function(e) {
    console.log(e.detail.value)
    this.setData({
      tel: e.detail.value
    })
  },
  password: function(e) {
    console.log(e.detail.value)
    this.setData({
      password: e.detail.value
    })
  },
  // 发送短信验证码
  sendSMS: function() {
    var that = this
    var mobile = this.data.tel
    console.log(mobile)
    var telRegex = /^1[3|4|5|6|7|8|9]\d{9}$/
    if (telRegex.test(mobile)) {
      wx.showLoading({
        title: '发送中,请稍后',
      })
      wx.request({
        url: 'https://miniapp.odeman.com/summercamp/get_mobile_code.php',
        data: {
          mobile: mobile
        },
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success(res) {
          wx.hideLoading()
          console.log(res.data)
          if (res.data.status == "success") {
            wxParse.countDown(that)
            wx.showToast({
              title: '验证码发送成功!',
              icon: 'success',
              duration: 3000
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'success',
              duration: 3000
            })
          }
        },
        fail(err) {
          wx.showModal({
            title: '',
            content: '验证码发送失败请稍后再试',
            success(res) {
              if (res.confirm) {} else if (res.cancel) {}
            }
          })
        }
      })
    } else {
      wx.showModal({
        title: '',
        content: '请输入正确的手机号'
      })
    }

  },
  // 获取验证码
  getCode: function(e) {
    var code = e.detail.value
    console.log(code)
    this.setData({
      code: code
    })
  },
  // 注册提交
  registerSubmit: function() {
    var that = this
    var telRegex = /^1[3|4|5|6|7|8|9]\d{9}$/
    var codeRegex = /^\d{6}$/
    var pwd = /^\S{6,18}$/g
    if (!telRegex.test(this.data.tel)) {
      wx.showModal({
        title: '',
        content: '请输入正确的手机号'
      })
    } else if (!codeRegex.test(this.data.code)) {
      wx.showModal({
        title: '',
        content: '请输入正确的验证码'
      })
    } else if (!pwd.test(this.data.password)) {
      wx.showModal({
        title: '',
        content: '请输入正确的6-18位密码'
      })
    } else {
      wx.request({
        url: 'https://miniapp.odeman.com/summercamp/register.php',
        data: {
          mobile: that.data.tel,
          yzmcode: that.data.code,
          password: that.data.password
        },
        method: 'post',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success(res) {
          console.log(res.data)
          if (res.data.status == "success") {
            wx.showToast({
              title: '登录成功',
              icon: 'success',
              duration: 2000
            })
            setTimeout(function(){
              wx.navigateTo({
                url: '/pages/login/login'
              })
            },2000)
          } else {
            wx.showModal({
              title: '',
              content: res.data.msg
            })
          }
        }
      })
    }
  }

})